<template>
  <div class="roleAdd">
    <el-form :model="formItem" ref="formItem" :rules="rules" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
            <el-input ref="roleName" v-model="formItem.roleName" autofocus=""></el-input>
        </el-form-item>
        <el-form-item label="角色值" prop="roleVal">
            <el-input ref="roleVal" v-model="formItem.roleVal"></el-input>
        </el-form-item>
        <!-- <el-form-item class="rolename" label="角色值" prop="roleVal">
            <Select class="form_add" ref="roleVal" v-model="formItem.roleVal">
              <Option v-for="item in formItem.roleValList" :value="item.roleVal" :key="item.roleVal">{{ item.roleVal }}</Option>
            </Select>
        </el-form-item> -->
        <el-form-item label="描        述" prop="description">
            <el-input ref="description" v-model="formItem.description"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnable">
            <el-switch ref="isEnable" v-model="formItem.isEnable"></el-switch>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" class="save" @click="add('formItem')">保存</el-button>
            <el-button class="quxiao" @click="closeLayer">取消</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "roleAdd",
  data() {
    return {
      // selectlist: {
      //   pageSize: 10,
      // },
      formItem: {
        roleName: "",
        roleVal: "",
        description: "",
        isEnable: true,
        roleValList: [] //存放角色值
      },
      rules: {
        roleName: [
          { required: true, message: "请填写角色名", trigger: "blur" },
          { min: 3, max: 20, message: "长度3-20", trigger: "blur" }
        ],
        roleVal: [
          { required: true, message: "请填选择色值", trigger: "blur" },
          { max: 20, message: "不能超出长度20", trigger: "blur" }
        ]
      },
    };
  },
  mounted: function() {
    this.$nextTick(() => {
      //获取全部角色
      // this.getRoles();
    });
  },
  methods: {
    //关闭layer弹窗
    closeLayer() {
      layer.closeAll();
    },
    // //获取全部角色
    // getRoles() {
    //   this.$nextTick(() => {
    //     axios
    //       .get(URL + "/api/userservice/v1/roles",{
    //         params: this.selectlist
    //       })
    //       .then(response => {
    //         let d = response.data;
    //         if (d.resultCode == 0) {
    //           this.formItem.roleValList = d.record.rows;
    //         } else {
    //           layer.msg(d.msg);
    //         }
    //       })
    //       .catch(error => {
    //         layer.msg("网络错误");
    //       });
    //   });
    // },
    //新增
    add(formName) {
      if (this.$refs.roleName.value == "") {
        layer.msg("请填写角色名");
        this.$refs.roleName.focus();
      } else if (this.$refs.roleVal.value == "") {
        layer.msg("请填选择色值");
      } else {
        axios
          .post(
            rolesURL,
            {
              roleName: this.formItem.roleName,
              roleVal: this.formItem.roleVal,
              isEnable: this.formItem.delivery,
              description: this.formItem.description
            },
            {
              headers: {'Authorization': appDB.get("userInfo").token},
            }
          )
          .then(response => {
            let d = response.data;
            if (d.resultCode == 0) {
              this.closeLayer(); //关闭弹窗
              this.$parent.getRoles(); //添加成功以后重新渲染列表获得最新数据
            } else {
              layer.msg(d.msg);
            }
          })
          .catch(error => {
            layer.msg("网络错误");
          });
      } //else
    }
  }
};
</script>

<style scoped>
.roleAdd {
  width: 80%;
  padding: 50px 10%;
  float: left;
}
.save {
  padding: 8px 40px;
  background: #38b824;
  border-color: #38b824;
  border-radius: 30px;
  float: right;
  margin-left: 30px;
}
.quxiao {
  border: 0;
  background: #cecece;
  padding: 8px 40px;
  border-color: #367fb6;
  border-radius: 30px;
  float: right;
}
</style>


